﻿using Microsoft.EntityFrameworkCore;
using NLog.Filters;
using NLog.Web;
using Sheep_Breeding.Domain.production_b;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Write.Api.Common;
using Sheep_Breeding.Write.Api.OperationServices.IServices;
using Sheep_Breeding.Write.Api.OperationServices.Services;

namespace Sheep_Breeding.Write.Api.Extensions
{
    /// <summary>
    /// 拓展类方法  静态类  静态方法  this关键字
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        public static WebApplicationBuilder Inject(this WebApplicationBuilder builder)
        {

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            //中文注释
            builder.Services.AddSwaggerGen(x =>
            {
                var file = Path.Combine(AppContext.BaseDirectory + "Sheep_Breeding.Write.Api.xml");
                x.IncludeXmlComments(file, true);
            });

            //配置EFcore数据库
            builder.Services.AddDbContext<EFDbContext>(x =>
            {
                x.UseMySql(builder.Configuration["ConnectionStrings:Conn"]
                , ServerVersion.AutoDetect(builder.Configuration["ConnectionStrings:Conn"]), action =>
                {
                    //配置EFCore的重试策略
                    action.EnableRetryOnFailure(
                        maxRetryCount: 10,//最大重试次数
                        maxRetryDelay: TimeSpan.FromSeconds(60),//每次重试间隔时间最大延迟
                        errorNumbersToAdd: null
                        );

                }).EnableSensitiveDataLogging();//启动敏感数据日志记录，使与调试 但在生产环境中应谨慎使用避免泄露敏感信息

                //配置日志记录
                x.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddFilter(
                    category: DbLoggerCategory.Database.Name,//类别
                    level: LogLevel.Information//控制台日志
                    ).AddConsole()));
            });


            //注入
            builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BasreRepository<>));

            //注入组织
            builder.Services.AddScoped<IOrganizationServices, OrganizationServices>();

            //注入羊场管理
            builder.Services.AddScoped<ISheepfarmServices,SheepfarmServices>();
            //注入羊只采购仓储
            builder.Services.AddScoped<SheepPurchase, SheepPurchase>();
            //注入羊只采购服务
            builder.Services.AddScoped<ISheepPurchaseServices, SheepPurchaseServices>();
            //注入羊场管理
            builder.Services.AddScoped<ISheepfarmServices, SheepfarmServices>();
            // 注入羊只销售服务
            builder.Services.AddScoped<ISheepSaleService, SheepSaleService>();
            // 注入羊只死亡服务
            builder.Services.AddScoped<ISheepDeathServices, SheepDeathServices>();

            //注入母羊配种服务
            builder.Services.AddScoped<IEweMatingServices, EweMatingServices>();

            //添加授权服务
            builder.Services.AddAuthorization();

            //初始化雪花ID生成器
            Yitter.IdGenerator.YitIdHelper.SetIdGenerator(new Yitter.IdGenerator.IdGeneratorOptions(1));


            //AutoMapper
            builder.Services.AddAutoMapper(typeof(MapperingProfil));
            //区分大小写
            builder.Services.AddControllersWithViews().AddJsonOptions(p => p.JsonSerializerOptions.PropertyNamingPolicy = null);
            //NLog注入
            builder.Host.UseNLog();
            return builder;
        }
    }
}
